package com.magic.smartcity.view.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.smartcity.R;
import com.magic.smartcity.beans.NewsCategoryBean;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/2/6.
 */
public class HomeNewsCategoryAdapter extends RecyclerView.Adapter<HomeNewsCategoryAdapter.ItemHolder> {
    private static final String TAG = HomeNewsCategoryAdapter.class.getSimpleName();
    List<NewsCategoryBean.DataBean> mDataBeanList = new ArrayList<>();
    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_news_category, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
        View itemView = holder.itemView;
        TextView textView = itemView.findViewById(R.id.text);
        textView.setText(mDataBeanList.get(position).getName());
        itemView.setOnClickListener(v -> {
            mOnClickItem.click(mDataBeanList.get(position).getId());
        });
    }

    @Override
    public int getItemCount() {
        return mDataBeanList.size();
    }

    public void setData(List<NewsCategoryBean.DataBean> dataBeanList) {
        mDataBeanList.clear();
        mDataBeanList.addAll(dataBeanList);
        notifyDataSetChanged();
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    private OnClickItem mOnClickItem = null;

    public void setOnClickItem(OnClickItem onClickItem) {
        mOnClickItem = onClickItem;
    }

    public interface OnClickItem{
        void click(int position);
    }
}
